package myNetty.action16;

import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.oio.OioSocketChannel;

import java.io.IOException;
import java.net.Socket;

/**
 * @author tonywang
 * @date 7/27/2018
 */
public class RegisterChannelOrSocket {
    public void registerChannel() throws IOException {
        java.nio.channels.SocketChannel mySocket = java.nio.channels.SocketChannel.open();
        //netty
        SocketChannel ch = new NioSocketChannel(mySocket);
        EventLoopGroup group = new NioEventLoopGroup();
        //register channel
        ChannelFuture registerFuture = group.register(ch);
        //de-register channel
        ChannelFuture deregisterFuture = ch.deregister();
    }
    public void registerSocket() throws IOException {
        //oio
        Socket mySocket = new Socket("www.baidu.com", 80);
        //netty
        SocketChannel ch = new OioSocketChannel(mySocket);
        EventLoopGroup group = new OioEventLoopGroup();
        //register channel
        ChannelFuture registerFuture = group.register(ch);
        //de-register channel
        ChannelFuture deregisterFuture = ch.deregister();
    }
}
